<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style></style>
    <style>
      .list {
        width: 400px;
      }
      .list li {
        border: solid;
        margin: 5px;
      }

      .list .active {
        background-color: beige;
      }
    </style>
  </head>
  <body>
    <ul class="list">
      <li data-item>css</li>
      <li data-item>html</li>
      <li data-item>js</li>
      <li data-item>vue</li>
    </ul>

    <script>
      document.onclick = function (e) {
        const item = e.target.dataset.item;
        if (item === undefined) return;

        if (e.ctrlKey === false) {
          //清除
          document
            .querySelectorAll(".list .active")
            .forEach((el) => el.classList.remove("active"));
        }
        //添加
        e.target.classList.add("active");
      };
    </script>
  </body>
</html>
